package com.u2b.servlet.utils;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import com.u2b.exceptions.ParameteException;
import com.u2b.global.utils.CheckParameter;
import com.u2b.global.utils.LoggerUtils;


public class ParameterParser
{

    public Object[] getSQLValuesFromMap(Object[] SQLkeys, Map<String, Object> value)
    {
        if (SQLkeys.length > value.keySet().size())
        {
            throw new ParameteException(this.getClass().getName() + "Map对象解析成SQLvalue出现异常");
        }
        ArrayList<Object> params = new ArrayList<Object>();
        for (int i = 0; i < SQLkeys.length; i++)
        {
            Object o = value.get(SQLkeys[i]);

            params.add(i, o);
        }
        return params.toArray();
    }

    public HashMap<String, Object> initFormRequest(HttpServletRequest request)
    {
        HashMap<String, Object> initParmas = new HashMap<String, Object>();
        Enumeration<String> paramsNames = request.getParameterNames();
        while (paramsNames.hasMoreElements())
        {
            String key = paramsNames.nextElement();
            String value = request.getParameter(key);
            if (CheckParameter.checkThisParamsIsNull(value))
            {
                initParmas.put(key, value);
                LoggerUtils.log("initFormRequest", key, value);
            }
            else
            {
                return null;
            }

        }

        return initParmas;
    }
}